/**
 * 获取触发器配置信息【注：不建议外部使用】
 */
def _getTriggerConfigInfo(key, triggerConfig) {
	
	def value = {listData->
	
		for(data in listData) {
			
			def type = data.get('type');
			
			if('object' == type || 'array' == type) {
				
				def value = call(data.children);
				
				if(value[0]) {
					return value;
				}
				
				continue;
			}
			
			if(key == data.key) {
				
				return [true, data];
			}
		}
		
		return [false, null];
		
	}(triggerConfig);
	
	
	return value[0] ? value[1] : null;
}


/**
 * 获取触发器配置值 【注：不建议外部使用】
 */
def _getTriggerConfigValue(key, defaultValue, triggerConfig) {
	
	def value = {listData->
	
		for(data in listData) {
			
			def type = data.get('type');
			
			if('object' == type || 'array' == type) {
				
				def value = call(data.children);
				
				if(value[0]) {
					return value;
				}
				
				continue;
			}
			
			if(key == data.key) {
				
				def inKey = data.get('inKey');
				
				if(StringHelper.isBlank(inKey)) {
					
					return [true, data.get('defaultValue')];
				}
				
				inKey = inKey.substring(inKey.indexOf('.') + 1);

				def value = null;
				
				if(null == type || 'string' == type) {
					
					value = getFieldValueForString(inKey);
					
				}else if('number' == type) {
					
					value = getFieldValueForNumber(inKey);
					
				}else if('date' == type) {
					
					value = getFieldValueForDateText(inKey);
					
				}else if('datetime' == type) {
					
					value = getFieldValueForTimeText(inKey);
					
				}else if('enum' == type) {
					
					value = getFieldValueForEnumShowvalue(inKey);
					
				}else if('enum_value' == type) {
					
					value = getFieldValueForEnumValue(inKey);
					
				}else if('enum_code' == type) {
					
					value = getFieldValueForEnumCode(inKey);
					
				}else if('member' == type) {
					
					value = getFieldValueForMember(inKey);
					
				}else if('member_code' == type) {
					
					value = getFieldValueForMemberCode(inKey);
					
				}else if('member_loginname' == type) {
					
					value = getFieldValueForMemberLoginName(inKey);
					
				}else if('department' == type) {
					
					value = getFieldValueForDeptName(inKey);
					
				}else if('department_code' == type) {
					
					value = getFieldValueForDeptCode(inKey);
					
				}else if('company' == type) {
					
					value = getFieldValueForUnitName(inKey);
					
				}else if('company_code' == type) {
					
					value = getFieldValueForUnitCode(inKey);
					
				}else if('post' == type) {
					
					value = getFieldValueForPostName(inKey);
					
				}else if('post_code' == type) {
					
					value = getFieldValueForPostCode(inKey);		
					
				}else if('level' == type) {
					
					value = getFieldValueForLevelName(inKey);
					
				}else if('level_code' == type) {
					
					value = getFieldValueForLevelCode(inKey);						
				}
				
				return [true, (StringHelper.isBlank(value) ? data.get('defaultValue') : value)];
			}
		}
		
		return [false, null];
		
	}(triggerConfig);
	
	
	return value[0] ? value[1] : defaultValue;
}

/**
 * 获取触发器配置值
 */
def getTriggerAttributeConfigValue(key, defaultValue=null, triggerActionContext=__TRIGGER_CONFIG__) {
	
	return _getTriggerConfigValue(key, defaultValue, triggerActionContext.attributeInfo);
}

/**
 * 获取触发器的业务数据对象
 */
def getTriggerBusinessBillsData(mergeConfig=null, triggerActionContext=__TRIGGER_CONFIG__) {
	
	def businessData = [:];
	def businessDataType = [:];
	
	{listData, mapData, mapDataType->
			
		for(data in listData) {
			
			def value = null;
			def type = data.get('type');
			
			mapDataType[data.key] = type;
			
			if('object' == type || 'array' == type) {
				
				call(data.children, mapData[data.key]=[:], mapDataType[data.key+'_type']=[:]);

				continue;
			}

			
			def inKey = data.get('inKey');
			
			if(StringHelper.isBlank(inKey)) {
				
				mapData[data.key] = data.get('defaultValue');;
				
				continue;
			}
			
			inKey = inKey.substring(inKey.indexOf('.') + 1);
			
			
			if(null == type || 'string' == type) {
				
				value = getFieldValueForString(inKey);
				
			}else if('number' == type) {
				
				value = getFieldValueForNumber(inKey);
				
			}else if('date' == type) {
				
				value = getFieldValueForDateText(inKey);
				
			}else if('datetime' == type) {
				
				value = getFieldValueForTimeText(inKey);
				
			}else if('enum' == type) {
				
				value = getFieldValueForEnumShowvalue(inKey);
				
			}else if('enum_value' == type) {
				
				value = getFieldValueForEnumValue(inKey);
				
			}else if('enum_code' == type) {
				
				value = getFieldValueForEnumCode(inKey);
				
			}else if('member' == type) {
				
				value = getFieldValueForMember(inKey);
				
			}else if('member_code' == type) {
				
				value = getFieldValueForMemberCode(inKey);
				
			}else if('member_loginname' == type) {
				
				value = getFieldValueForMemberLoginName(inKey);
				
			}else if('department' == type) {
				
				value = getFieldValueForDeptName(inKey);
				
			}else if('department_code' == type) {
				
				value = getFieldValueForDeptCode(inKey);
				
			}else if('company' == type) {
				
				value = getFieldValueForUnitName(inKey);
				
			}else if('company_code' == type) {
				
				value = getFieldValueForUnitCode(inKey);
				
			}else if('post' == type) {
				
				value = getFieldValueForPostName(inKey);
				
			}else if('post_code' == type) {
				
				value = getFieldValueForPostCode(inKey);		
				
			}else if('level' == type) {
				
				value = getFieldValueForLevelName(inKey);
				
			}else if('level_code' == type) {
				
				value = getFieldValueForLevelCode(inKey);	
			}
			
			mapData[data.key] = (StringHelper.isBlank(value) ? data.get('defaultValue') : value);
		}
		
	}(triggerActionContext.fieldInfo, businessData, businessDataType);
	
	
	// 拆解数据
	def disassembleData = {mapData->

		def size = 0;
		
		for(e in mapData) {
			
			if(e.value instanceof java.util.List) {
				
				size = (size < e.value.size() ? e.value.size() : size);
			}
		}
		
		def newData = null;
		
		if(0 < size) {
			
			newData = [];
			
			(0..<size).each{i->
			
				newData.add([:]);
			
				({_mapData, _newMapData, _i->
				
					_mapData.each{key,value->
					
						if(value instanceof java.util.List) {
							
							_newMapData[key] = (_i < value.size() ? value[_i] : null);
							
						}else {
							_newMapData[key] = value;
						}
					}
				
				})(mapData, newData[-1], i);
			}
		} else {
			newData = [mapData];
		}
		
		return newData;
	}
	

	
	def businessDatas = [];
	
	{mapData, mapDataType, listData->

		listData.addAll(disassembleData(mapData));
	
		for(data in listData) {
			
			for(k in data.keySet()) {
				
				if(data[k] instanceof java.util.Map) {
									
					call(data[k], mapDataType[k+'_type'], data[k]=[]);
					
					if('object' == mapDataType[k]) {
						
						if(0 < data[k].size()) {
							data[k] = data[k][0];
						}
					}
				}
			}
		}	
	}(businessData, businessDataType, businessDatas);
	
	// 合并
	if(null != mergeConfig) {
		
		
		mergeConfig.each{mergeKey, mergeInfo->
			
			// 1. 去多余数据
			{_mergeKey, _mergeInfo, listData->
				
				for(data in listData) {
					
					for(mainData in data[_mergeKey]) {
						
						def itemData = mainData[_mergeInfo.sub];
						
						for(def i=0; i<itemData.size(); i++) {
							
							def matching = true;
							
							for(mpg in _mergeInfo.mapping) {
								
								if((mainData.get(mpg[0]) as String) != (itemData[i].get(mpg[1]) as String)) {
									matching = false;
									break;
								}
							}
							
							if(matching) {
								continue;
							}
							
							itemData.remove(i);
							
							i--;
						}
					}
				}	
			}(mergeKey, mergeInfo, businessDatas);
			
			// 2. 合并
			{_mergeKey, _mergeInfo, listData->
			
				for(data in listData) {
					
					def itemsData = data[_mergeKey];
					
					for(def i=0; i<itemsData.size(); i++) {
						
						for(def j=i+1; j<itemsData.size(); j++) {
							
							def matching = true;
							
							for(mpg in _mergeInfo.mapping) {
								
								if((itemsData[i].get(mpg[0]) as String) != (itemsData[j].get(mpg[0]) as String)) {
									matching = false;
									break;
								}
							}
							
							if(!matching) {
								continue;
							}
																
							itemsData.remove(j);
							
							j--;
						}
					}
				}	
			}(mergeKey, mergeInfo, businessDatas);
			
		}		
	}

	if(0 == businessDatas.size()) {
		
		return null;
		
	}else if(1 == businessDatas.size()) {
		
		return businessDatas[0];
	}
	
	return businessDatas;
}

/**
 * 更新表单中业务数据（数据表）
 */
def setTriggerBusinessBillsData(key, value, triggerActionContext=__TRIGGER_CONFIG__) {
	
	def fieldInfo = _getTriggerConfigInfo(key, triggerActionContext.fieldInfo);
	
	if(null == fieldInfo) {
		
		logger("${key}, 不存在！");
		
		return null;
	}
	
	def inKey = fieldInfo.get('inKey');
	
	if(null == inKey) {
		
		logger("${key}无配置值！");
		
		return null;
	}
	
	setFieldValue(inKey.substring(inKey.indexOf('.') + 1), value);
}

/**
 * 更新表单中业务配置数据（数据表）
 */
def setTriggerBusinessAttributeData(key, value, triggerActionContext=__TRIGGER_CONFIG__) {
	
	def fieldInfo = _getTriggerConfigInfo(key, triggerActionContext.attributeInfo);
	
	if(null == fieldInfo) {
		
		logger("${key}，不存在！");
		
		return null;
	}
	
	def inKey = fieldInfo.get('inKey');
	
	if(null == inKey) {
		
		logger("${key}无配置值！");
		
		return null;
	}
	
	setFieldValue(inKey.substring(inKey.indexOf('.') + 1), value);
}


